﻿using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using Secured3d.Auto.Pages;
using Secured3d.Auto.Utils;

namespace Secured3d.Auto.Helpers
{
    public class Menu : BasePage
    {
        //Locators
        [FindsBy(How = How.Id, Using = "menuitem_upload")] 
        private IWebElement uploadLink = null;

        [FindsBy(How = How.Id, Using = "menuitem_myfiles")] 
        private IWebElement myFilesLink = null;

        [FindsBy(How = How.Id, Using = "menuitem_dashboard")] 
        private IWebElement dashboardLink = null;

        [FindsBy(How = How.Id, Using = "menuitem_printing")] 
        private IWebElement printersLink = null;

        [FindsBy(How = How.ClassName, Using = "menu")] 
        private IWebElement menuBlock = null;

        [FindsBy(How = How.Id, Using = "menuitem_user")] 
        private IWebElement userNameLabel = null;

        [FindsBy(How = How.CssSelector, Using = "#user_submenu a[href=\"/logout\"]")] 
        private IWebElement logoutButton = null;

        public void GoTo(To to)
        {
            switch (to)
            {
                case To.UploadFile:
                    uploadLink.WaitUntilVisible().Click();
                    break;
                case To.MyFiles:
                    myFilesLink.WaitUntilVisible().Click();
                    break;
                case To.Dashboard:
                    dashboardLink.WaitUntilVisible().Click();
                    break;
                case To.Printers:
                    printersLink.WaitUntilVisible().Click();
                    break;
            }
        }

        public string AuthorizedUser
        {
            get { return userNameLabel.WaitUntilVisible().Text; }
        }

        public bool IsVisible
        {
            get
            {
                try
                {
                    return menuBlock.Displayed;
                }
                catch (NoSuchElementException exception)
                {
                    return false;
                }
            }
        }

        public void LogOut()
        {
            userNameLabel.WaitUntilVisible().Click();
            Thread.Sleep(1000);
            logoutButton.WaitUntilVisible().Click();
        }

    }

    public enum To
    {
        UploadFile
        ,MyFiles
        ,Dashboard
        ,Printers
    }
}
